/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * POS_ViewCart.java
 *
 * Created on Apr 4, 2011, 7:13:38 PM
 */
package POS_Source;

import Cart.Cart;
import DTO.Item;
import Exception.KwickKioskDBException;
import POS_Controller.POSController;
import Utilities.MoneyFormatter;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 * this class renders view cart
 * @author edaype
 * @see POS_Source
 */
public class POS_ViewCart extends javax.swing.JPanel {

    private Cart cart;
    private POSController controller;
    private static final String[] columnNames = new String[]{"Name", "Quantity", "Price"};
    private MoneyFormatter moneyFormatter;

    /** Creates new form POS_MainOut
     * @param control
     * @param cart 
     */
    public POS_ViewCart(POSController control) {
        initComponents();
        controller = control;
        this.cart = control.getSessionManager().getCart();
        moneyFormatter = new MoneyFormatter();
        customInit();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        AddBagButton = new javax.swing.JButton();
        DecreaseButton = new javax.swing.JButton();
        IncreaseButton = new javax.swing.JButton();
        DeleteButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        TotalPanel = new javax.swing.JPanel();
        SubtotalLabel = new javax.swing.JLabel();
        TaxLabel = new javax.swing.JLabel();
        DivisionLabel = new javax.swing.JLabel();
        TotalLabel = new javax.swing.JLabel();
        SubTotalAmount = new javax.swing.JLabel();
        TaxAmount = new javax.swing.JLabel();
        TotalAmount = new javax.swing.JLabel();
        TotalDiscountLabel = new javax.swing.JLabel();
        DiscountAmount = new javax.swing.JLabel();
        StoreCardDiscountAmount = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        ClerkOptionPane = new javax.swing.JPanel();
        DiscountButton = new javax.swing.JButton();
        OverrideButton = new javax.swing.JButton();
        ButtonLayout = new javax.swing.JPanel();
        TitleLabel = new javax.swing.JLabel();
        CheckoutButton = new javax.swing.JButton();
        ErrorLabel = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setMaximumSize(new java.awt.Dimension(800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));

        AddBagButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        AddBagButton.setText("Add Bag");
        AddBagButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddBagButtonActionPerformed(evt);
            }
        });

        DecreaseButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        DecreaseButton.setText("-");
        DecreaseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DecreaseButtonActionPerformed(evt);
            }
        });

        IncreaseButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        IncreaseButton.setText("+");
        IncreaseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                IncreaseButtonActionPerformed(evt);
            }
        });

        DeleteButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        DeleteButton.setText("Delete");
        DeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteButtonActionPerformed(evt);
            }
        });

        jTable1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Name", "Make", "Model", "Price", "Quantity"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 634, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(IncreaseButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                    .addComponent(DeleteButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                    .addComponent(AddBagButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(DecreaseButton, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addComponent(IncreaseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DecreaseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DeleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(AddBagButton, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        TotalPanel.setBackground(new java.awt.Color(255, 255, 255));

        SubtotalLabel.setFont(new java.awt.Font("Tahoma", 0, 13));
        SubtotalLabel.setText("Subtotal");

        TaxLabel.setFont(new java.awt.Font("Tahoma", 0, 13));
        TaxLabel.setText("Tax");

        DivisionLabel.setText("_____________________________________");

        TotalLabel.setFont(new java.awt.Font("Tahoma", 0, 15));
        TotalLabel.setText("Total");

        SubTotalAmount.setFont(new java.awt.Font("Tahoma", 0, 13));
        SubTotalAmount.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        SubTotalAmount.setText(" ");

        TaxAmount.setFont(new java.awt.Font("Tahoma", 0, 13));
        TaxAmount.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        TaxAmount.setText(" ");

        TotalAmount.setFont(new java.awt.Font("Tahoma", 0, 15));
        TotalAmount.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

        TotalDiscountLabel.setFont(new java.awt.Font("Tahoma", 0, 13));
        TotalDiscountLabel.setText("Total Discount");

        DiscountAmount.setFont(new java.awt.Font("Tahoma", 0, 13));
        DiscountAmount.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        DiscountAmount.setText(" ");

        StoreCardDiscountAmount.setFont(new java.awt.Font("Tahoma", 0, 13));
        StoreCardDiscountAmount.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        StoreCardDiscountAmount.setText(" ");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel7.setText("Storecard Discount");

        javax.swing.GroupLayout TotalPanelLayout = new javax.swing.GroupLayout(TotalPanel);
        TotalPanel.setLayout(TotalPanelLayout);
        TotalPanelLayout.setHorizontalGroup(
            TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TotalPanelLayout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(DivisionLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(TotalPanelLayout.createSequentialGroup()
                        .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(TaxLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(SubtotalLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(TotalDiscountLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(TaxAmount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(SubTotalAmount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(DiscountAmount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(StoreCardDiscountAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(37, 37, 37))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, TotalPanelLayout.createSequentialGroup()
                .addContainerGap(93, Short.MAX_VALUE)
                .addComponent(TotalLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TotalAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47))
        );
        TotalPanelLayout.setVerticalGroup(
            TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, TotalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(StoreCardDiscountAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TotalDiscountLabel)
                    .addComponent(DiscountAmount))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SubtotalLabel)
                    .addComponent(SubTotalAmount))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TaxLabel)
                    .addComponent(TaxAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(1, 1, 1)
                .addComponent(DivisionLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(TotalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(TotalAmount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(TotalLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(13, 13, 13))
        );

        ClerkOptionPane.setBackground(new java.awt.Color(255, 255, 255));

        DiscountButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        DiscountButton.setText("Apply Discount");
        DiscountButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DiscountButtonActionPerformed(evt);
            }
        });

        OverrideButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        OverrideButton.setText("Override Price");
        OverrideButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OverrideButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ClerkOptionPaneLayout = new javax.swing.GroupLayout(ClerkOptionPane);
        ClerkOptionPane.setLayout(ClerkOptionPaneLayout);
        ClerkOptionPaneLayout.setHorizontalGroup(
            ClerkOptionPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ClerkOptionPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(DiscountButton, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(OverrideButton, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        ClerkOptionPaneLayout.setVerticalGroup(
            ClerkOptionPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ClerkOptionPaneLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(ClerkOptionPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DiscountButton, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(OverrideButton, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        ButtonLayout.setBackground(new java.awt.Color(255, 255, 255));

        TitleLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 55));
        TitleLabel.setText("Cart Review");

        CheckoutButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        CheckoutButton.setText("Checkout");
        CheckoutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CheckoutButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ButtonLayoutLayout = new javax.swing.GroupLayout(ButtonLayout);
        ButtonLayout.setLayout(ButtonLayoutLayout);
        ButtonLayoutLayout.setHorizontalGroup(
            ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonLayoutLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TitleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 426, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 202, Short.MAX_VALUE)
                .addComponent(CheckoutButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        ButtonLayoutLayout.setVerticalGroup(
            ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonLayoutLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(CheckoutButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TitleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        ErrorLabel.setFont(new java.awt.Font("Tahoma", 3, 18)); // NOI18N
        ErrorLabel.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addComponent(TotalPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ErrorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(ButtonLayout, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(ClerkOptionPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(17, 17, 17))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ClerkOptionPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(ErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(TotalPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ButtonLayout, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * this method handles add to cart button action event
     * @param evt
     */
    private void IncreaseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IncreaseButtonActionPerformed
        ErrorLabel.setText("");
        int selected = jTable1.getSelectedRow();
        if (selected != -1) {
            String selectedName = jTable1.getValueAt(selected, 0).toString();
            if (!selectedName.equals("Discount")) {
                int index = cart.find(selectedName);
                try {
                    cart.increaseQuantity(index);
                } catch (KwickKioskDBException ex) {
                    Logger.getLogger(POS_ViewCart.class.getName()).log(Level.SEVERE, null, ex);
                    controller.DatabaseDownScreen();
                }
                Item item = cart.getItemsInCart().get(index);
                jTable1.setValueAt(item.getQty(), selected, 1);
                jTable1.setValueAt(moneyFormatter.format(item.getSoldPrice() * item.getQty()), selected, 2);
                if (item.getDiscountAmount() > 0.0 && controller.getSessionManager().getLoggedIn()) {
                    jTable1.setValueAt(item.getQty(), selected + 1, 1);
                    jTable1.setValueAt("-" + moneyFormatter.format(item.getDiscountAmount() * item.getQty()), selected + 1, 2);
                }
                setTotals();
                jTable1.repaint();
                controller.refresh();
            }
        } else {
            ErrorLabel.setText("No Item Selected.");
        }
    }//GEN-LAST:event_IncreaseButtonActionPerformed
    /**
     * this method decrements  an item from cart by handling this event
     * @param evt
     */
    private void DecreaseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DecreaseButtonActionPerformed
        ErrorLabel.setText("");
        int selected = jTable1.getSelectedRow();
        if (selected != -1) {
            String selectedName = jTable1.getValueAt(selected, 0).toString();
            if (!selectedName.equals("Discount")) {
                int index = cart.find(selectedName);
                try {
                    cart.decreaseQuantity(index);
                } catch (KwickKioskDBException ex) {
                    Logger.getLogger(POS_ViewCart.class.getName()).log(Level.SEVERE, null, ex);
                    controller.DatabaseDownScreen();
                }
                Item item = cart.getItemsInCart().get(index);
                jTable1.setValueAt(item.getQty(), selected, 1);
                jTable1.setValueAt(moneyFormatter.format(item.getSoldPrice() * item.getQty()), selected, 2);
                if (item.getDiscountAmount() > 0.0 && controller.getSessionManager().getLoggedIn()) {
                    jTable1.setValueAt(item.getQty(), selected + 1, 1);
                    jTable1.setValueAt("-" + moneyFormatter.format(item.getDiscountAmount() * item.getQty()), selected + 1, 2);
                }
                setTotals();
                jTable1.repaint();
                controller.refresh();
            }
        } else {
            ErrorLabel.setText("No Item Selected.");
        }
    }//GEN-LAST:event_DecreaseButtonActionPerformed
    /**
     * this method deletes an item from cart
     * @param evt
     */
    private void DeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteButtonActionPerformed
        ErrorLabel.setText("");
        int selected = jTable1.getSelectedRow();
        if (selected != -1) {
            String selectedName = jTable1.getValueAt(selected, 0).toString();
            if (!selectedName.equals("Discount")) {
                int index = cart.find(selectedName);
                try {
                    cart.removeItem(index);
                } catch (KwickKioskDBException ex) {
                    Logger.getLogger(POS_ViewCart.class.getName()).log(Level.SEVERE, null, ex);
                    controller.DatabaseDownScreen();
                }
                controller.CartScreen();
            }
        } else {
            ErrorLabel.setText("No Item Selected.");
        }

    }//GEN-LAST:event_DeleteButtonActionPerformed
    /**
     * this method handles checkout button event and directs the control to controller
     * @param evt
     */
    private void CheckoutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CheckoutButtonActionPerformed
        if (!controller.getSessionManager().getCart().getItemsInCart().isEmpty()) {
            ArrayList<Item> itemsInCart = (ArrayList<Item>) controller.getSessionManager().getCart().getItemsInCart().clone();
            boolean available = true;
            for (Item item : itemsInCart) {
                int qty = item.getQty();
                System.out.println("qty " + qty);
                String upc = item.getUpc();
                if (!controller.quantityAvailable(upc, qty)) {
                    available = false;
                    break;
                }
            }
            if (available) {
                controller.PaymentSelectScreen();
            } else {
                ErrorLabel.setText("Sorry, quantity requested is not available");
            }
        } else {
            ErrorLabel.setText("No Items in Cart.");
        }
    }//GEN-LAST:event_CheckoutButtonActionPerformed
    /**
     * this method handles apply discount button press event
     * @param evt
     */
    private void DiscountButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DiscountButtonActionPerformed
        if (!controller.getSessionManager().getCart().getItemsInCart().isEmpty()) {
            controller.ClerkDiscountScreen();
        } else {
            ErrorLabel.setText("No Items in Cart.");
        }
    }//GEN-LAST:event_DiscountButtonActionPerformed
    /**
     * this method handles override price button press event
     * @param evt
     */
    private void OverrideButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OverrideButtonActionPerformed
        int selected = jTable1.getSelectedRow();
        if (selected != -1) {
            String selectedName = jTable1.getValueAt(selected, 0).toString();
            if (!selectedName.equals("Discount")) {
                int index = cart.find(selectedName);
                Item item = cart.getItemsInCart().get(index);
                controller.OverridePriceScreen(item);
            }
        } else {
            ErrorLabel.setText("No Item Selected.");
        }

    }//GEN-LAST:event_OverrideButtonActionPerformed
    /**
     * this method handles add bas button press event
     * @param evt
     */
    private void AddBagButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddBagButtonActionPerformed
        Item bag = new Item("1", "Plastic Bag", "", "",
                1, 0.05);
        controller.addItemToCart(bag, 1);
        controller.CartScreen();
    }//GEN-LAST:event_AddBagButtonActionPerformed
    /**
     * this method overrides default view rendering
     */
    public final void customInit() {
        initTable();
        setTotals();
        if (!controller.getSessionManager().isClerkLoggedIn()) {
            OverrideButton.setVisible(false);
            DiscountButton.setVisible(false);
        }

    }

    /**
     * this method initializes  and renders a cart table
     */
    private void initTable() {

        ListIterator<Item> iter = cart.getItemsInCart().listIterator();
        DefaultTableModel model = new DefaultTableModel(columnNames, 0);

        while (iter.hasNext()) {
            Item item = iter.next();
            Vector row = new Vector();
            row.add(item.getName());
            row.add(item.getQty());
            row.add(moneyFormatter.format(item.getSoldPrice() * item.getQty()));
            model.addRow(row);
            if (item.getDiscountAmount() > 0.0 && controller.getSessionManager().getLoggedIn()) {
                row = new Vector();
                row.add("Discount");
                row.add(item.getQty());
                row.add("-" + moneyFormatter.format(item.getDiscountAmount() * item.getQty()));
                model.addRow(row);

            }
        }

        jTable1.setModel(model);

        TableColumn column = null;
        column = jTable1.getColumnModel().getColumn(0);
        column.setPreferredWidth(375); //third column is bigger
        column = jTable1.getColumnModel().getColumn(1);
        column.setPreferredWidth(25);
        column = jTable1.getColumnModel().getColumn(2);
        column.setPreferredWidth(50);

        jTable1.setRowHeight(40);

        jTable1.setColumnSelectionAllowed(false);
        jTable1.setRowSelectionAllowed(true);
    }

    /**
     * this method updates dynamic receipt
     */
    private void setTotals() {
        StoreCardDiscountAmount.setText(moneyFormatter.format(cart.getStoreCardDiscount()));
        DiscountAmount.setText(moneyFormatter.format(cart.getTotalDiscount()));
        SubTotalAmount.setText(moneyFormatter.format(cart.getSubtotal()));
        TaxAmount.setText(moneyFormatter.format(cart.getTaxAmount()));
        TotalAmount.setText(moneyFormatter.format(cart.getTotal()));
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddBagButton;
    private javax.swing.JPanel ButtonLayout;
    private javax.swing.JButton CheckoutButton;
    private javax.swing.JPanel ClerkOptionPane;
    private javax.swing.JButton DecreaseButton;
    private javax.swing.JButton DeleteButton;
    private javax.swing.JLabel DiscountAmount;
    private javax.swing.JButton DiscountButton;
    private javax.swing.JLabel DivisionLabel;
    private javax.swing.JLabel ErrorLabel;
    private javax.swing.JButton IncreaseButton;
    private javax.swing.JButton OverrideButton;
    private javax.swing.JLabel StoreCardDiscountAmount;
    private javax.swing.JLabel SubTotalAmount;
    private javax.swing.JLabel SubtotalLabel;
    private javax.swing.JLabel TaxAmount;
    private javax.swing.JLabel TaxLabel;
    private javax.swing.JLabel TitleLabel;
    private javax.swing.JLabel TotalAmount;
    private javax.swing.JLabel TotalDiscountLabel;
    private javax.swing.JLabel TotalLabel;
    private javax.swing.JPanel TotalPanel;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
